package com.jml.zlhq.app.ui;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.youmi.android.listener.Interface_ActivityListener;
import net.youmi.android.offers.OffersBrowserConfig;
import net.youmi.android.offers.OffersManager;

import org.json.JSONArray;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.dow.android.DOW;
import cn.waps.AppConnect;

import com.bb.dd.BeiduoPlatform;
import com.chuannuo.tangguo.TangGuoWall;
import com.dc.wall.DianCai;
import com.dlnetwork.DevInit;
import com.jml.zlhq.app.AppConfig;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.R;
import com.jml.zlhq.app.adapter.DownTaskAdapter;
import com.jml.zlhq.app.adapter.IBaseAdapter;
import com.jml.zlhq.app.util.SharedPreferenceHelper;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.CommonDialog;
import com.newqm.pointwall.QEarnNotifier;
import com.newqm.pointwall.QSdkManager;
import com.yql.dr.sdk.DRSdk;
import com.zkmm.appoffer.ZkmmAppOffer;
import com.zy.phone.SDKInit;

public class TanDianFragment extends BaseFragment implements
		OnItemClickListener, QEarnNotifier {
	public static final float txxz = 500.0f;
	public static final String txxzstr = "500金币";
	int falg;
	private ListView listview;
	private IBaseAdapter adapter;

	private ZkmmAppOffer appOffer;
	public static final List<Object[]> downtask = new ArrayList<Object[]>();
	static {
		downtask.add(new Object[] { "diancai", "点财通道", R.drawable.logo_diancai,
				"天天送钱，应用零点更新" });
//		downtask.add(new Object[] { "qumi", "趣米通道", R.drawable.logo_qumi,
//				"天天送钱，应用零点更新" });
//		downtask.add(new Object[] { "tangguo", "糖果通道", R.drawable.logo_tangguo,
//				"天天送钱，应用零点更新" });
//		downtask.add(new Object[] { "dianle", "点乐通道", R.drawable.dianjoy_logo,
//				"天天送钱，应用零点更新" });
//		downtask.add(new Object[] { "zhongyi", "中亿通道", R.drawable.logo_zhognyi,
//				"天天送钱，应用零点更新" });
//
////		downtask.add(new Object[] { "anwo", "安沃通道", R.drawable.logo_anwo,
////				"天天送钱，应用零点更新" });
//		downtask.add(new Object[] { "youmi", "有米通道", R.drawable.logo_youmi,
//				"天天送钱，应用零点更新" });
//		downtask.add(new Object[] { "baidu", "百度通道", R.drawable.logo_baidu,
//				"实力强劲，给您送钱" });
//		downtask.add(new Object[] { "wanpu", "万普通道", R.drawable.logo_wanpu,
//				"万里通达，普惠大众" });
//		downtask.add(new Object[] { "beiduo", "贝多通道", R.drawable.logo_beiduo,
//				"这里宝贝多多，速速前来" });
//		downtask.add(new Object[] { "duomeng", "多盟通道", R.drawable.logo_duomeng,
//				"多盟多萌，送钱就我最萌" });
//		downtask.add(new Object[] { "dianru", "点入通道", R.drawable.logo_dianru,
//				"点击进入，开始赚钱" });
		// downtask.add(new
		// Object[]{"anwo","安沃通道",R.drawable.logo_duomeng,"安沃多萌，送钱就我最萌"});
		// downtask.add(new
		// Object[]{"wanpu","万普通道",R.drawable.logo_duomeng,"万普多萌，送钱就我最萌"});
	}
	private List<Object[]> dwonItems = new LinkedList<Object[]>();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		// 2.安沃
		appOffer = ZkmmAppOffer.getInstance(getActivity(),
				"3ff72c43e7d4426392b1fd561bff527d");
		LinearLayout leftLay = (LinearLayout) pageView
				.findViewById(R.id.content_header_left_lay);
		LinearLayout rightLay = (LinearLayout) pageView
				.findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		rightLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) pageView
				.findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) pageView
				.findViewById(R.id.content_header_left_img);
		leftImage.setVisibility(View.INVISIBLE);
		ImageView roghtImage1 = (ImageView) pageView
				.findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.INVISIBLE);
		TextView centerText = (TextView) pageView
				.findViewById(R.id.content_header_center_text);
		centerText.setText("下载赚钱");
		TextView rightText = (TextView) pageView
				.findViewById(R.id.content_header_right_text);
		rightText.setVisibility(View.INVISIBLE);
		TextView centerChosed = (TextView) pageView
				.findViewById(R.id.content_header_center_text2);
		centerChosed.setVisibility(View.GONE);

		listview = (ListView) pageView.findViewById(R.id.main_tandian_list);
		adapter = new DownTaskAdapter(activity, dwonItems);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(this);
		falg = new SharedPreferenceHelper(getActivity(), "one").getFlag();

		loadData(null);
		initDown();
		if (falg == -1) {
			UIUtil.Tongzhi(getActivity());
		}
		return pageView;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 105) {
			if (!appContext.isLogin()) {
				UIUtil.ToastMessage(activity, "未登录!");
			} else {
				com.baidu.mobads.appoffers.OffersManager.setUserName(activity,
						appContext.getLoginUid());
				DOW.getInstance(activity).setUserId(appContext.getLoginUid());
				BeiduoPlatform.setUserId(appContext.getLoginUid());
				OffersManager.getInstance(activity).setCustomUserId(
						appContext.getLoginUid());
			}
		}
	}

	public void initDown() {
		String adsp = AppConfig.getAppConfig(activity).get("adsp");

		dwonItems.clear();
		try {
			JSONArray adspArr = new JSONArray(adsp);// 正式

			// JSONArray adspArr = new JSONArray(); //测试
			// adspArr.put(6, "youmi");
			// adspArr.put(5, "baidu");
			// adspArr.put(4, "wanpu");
			// adspArr.put(3, "beiduo");
			// adspArr.put(2, "duomeng");
			// adspArr.put(1, "dianru");
			// adspArr.put(0, "anwo");

			for (int i = 0; i < TanDianFragment.downtask.size(); i++) {
				String tag = TanDianFragment.downtask.get(i)[0].toString();
				// for (int j = 0; adspArr!=null && j < adspArr.length(); j++) {
				// if(tag.equals(adspArr.getString(j))){
				// Object[] ttt = TanDianFragment.downtask.get(i);
				// dwonItems.add(ttt);
				// break;
				// }
				// }
				Object[] ttt = TanDianFragment.downtask.get(i);
				dwonItems.add(ttt);
			}
			adapter.notifyDataSetChanged();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void loadData(Map<String, String> param) {

	}

	@Override
	public void onClick(View v) {

	}

	@Override
	protected int getPageView() {
		return R.layout.main_tandian;
	}

	@Override
	public void clearData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			final int position, final long id) {
		String downwarming = appContext.getProperty("downwarming");
		if (StringUtil.isEmpty(downwarming) || !downwarming.equals("ok")) {
			CommonDialog dialog = new CommonDialog(activity, true);
			dialog.show(
					true,
					true,
					true,
					"友情提示",
					"赚零花钱建议您安装一款安全软件，比如腾讯管家等，以防止流量偷跑，防止手机中毒，防止后台发短信，避免恶意扣话费。<br><font color='red'>如果体验应用时遇到要发短信激活，请拒绝！</font>",
					"不再提醒", "知道了", new android.view.View.OnClickListener() {
						@Override
						public void onClick(View view) {
							((CommonDialog) view.getTag()).dismiss();
							// LingGanFragment.downitemEvt(dwonItems,
							// appContext, activity, TanDianFragment.this,
							// position, id);
							downitemEvt(dwonItems, appContext, activity,
									TanDianFragment.this, position, id);
						}
					}, new android.view.View.OnClickListener() {
						@Override
						public void onClick(View view) {
							((CommonDialog) view.getTag()).dismiss();
							appContext.setProperty("downwarming", "ok");
							// LingGanFragment.downitemEvt(dwonItems,
							// appContext, activity, TanDianFragment.this,
							// position, id);
							downitemEvt(dwonItems, appContext, activity,
									TanDianFragment.this, position, id);

						}
					});
		} else {
			downitemEvt(dwonItems, appContext, activity, TanDianFragment.this,
					position, id);
		}

	}

	public void downitemEvt(List<Object[]> items, AppContext appContext,
			Activity activity, Fragment fgt, int position, long id) {
		int index = new Long(id).intValue();
		Log.e("name", items.get(index)[0].toString());
		if (items.get(index)[0].toString().equals("youmi")) {
			if (appContext.isLogin()) {
				OffersBrowserConfig.setBrowserTitleText("赚零花钱");
				OffersBrowserConfig.setBrowserTitleBackgroundColor(activity
						.getResources().getColor(R.color.color_e95847));
				OffersBrowserConfig.setPointsLayoutVisibility(false);
				OffersManager.getInstance(activity).showOffersWall(
						new Interface_ActivityListener() {
							@Override
							public void onActivityDestroy(Context arg0) {
							}
						});
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("baidu")) {
			if (appContext.isLogin()) {
				UIUtil.openActivity(fgt, ShowInActivity.class, null);
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("dianru")) {
			if (appContext.isLogin()) {

				DRSdk.showOfferWall(activity, DRSdk.DR_OFFER);

			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("beiduo")) {
			if (appContext.isLogin()) {
				// DRSdk.showOfferWall(activity, DRSdk.DR_OFFER);
				// Intent intent = new Intent(activity, DRActivity.class);
				// intent.putExtra(DRSdk.DR_TYPE, DRSdk.DR_OFFER);
				// activity.startActivity(intent);
				BeiduoPlatform.showOfferWall(activity);
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("duomeng")) {
			if (appContext.isLogin()) {
				DOW.getInstance(activity).show(activity);
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("wanpu")) {
			if (appContext.isLogin()) {
				AppConnect.getInstance(activity).showOffers(activity,
						appContext.getLoginUid());
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("anwo")) {
			if (appContext.isLogin()) {
				MainActivity mActivity = (MainActivity) activity;
				mActivity.AnWoAppOffer.showOffer(activity);
				// ZkmmAppOffer.showOffer(activity);
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("diancai")) {
			if (appContext.isLogin()) {
				DianCai.showOfferWall();
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("anwo")) {
			// 安沃
			if (appContext.isLogin()) {
				appOffer.showOffer(getActivity());
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("zhongyi")) {
			// 中亿
			if (appContext.isLogin()) {
				SDKInit.initAdList(activity);
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("dianle")) {
			// 点乐
			if (appContext.isLogin()) {
				DevInit.showOffers(activity);
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("tangguo")) {
			// 糖果
			if (appContext.isLogin()) {
				TangGuoWall.show();
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		} else if (items.get(index)[0].toString().equals("qumi")) {
			// 趣米
			if (appContext.isLogin()) {
				QSdkManager.getsdkInstance().showOffers(this);
			} else {
				UIUtil.openActivity(fgt, LoginActivity.class, null, 105);
			}
		}
	}

	@Override
	public void earnedPoints(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void getPoints(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void getPointsFailed(String arg0) {
		// TODO Auto-generated method stub

	}
}
